Adding a PhonePe payment gateway to a website isn’t just about using PHP code. The process involves several steps, like getting a merchant account for handling payments, getting essential API credentials, and setting up how the payment gateway works.
Suppose you already have a merchant account and API credentials for your PhonePe gateway. In that case, you can follow this example to connect your Phone Pay payment gateway to your PHP website:
ALSO READ: Authorize.Net Payment Gateway Integration using PHP
Before starting first, create your merchant account with PhonePe https://www.phonepe.com/business-solutions/payment-gateway/register/
PhonePe API Credentials
2 3 4 5 6 |
// Replace these with your actual PhonePe API credentials $apiKey = 'YOUR_API_KEY'; $merchantId = 'YOUR_MERCHANT_ID'; |
Step 2: Payment request data (Payload)
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Prepare the payment request data (you should customize this) $paymentData = array( 'merchantId' => $merchantId, 'merchantTransactionId' => "ORDER_TRANSACTION_UNIQUE_ID", "merchantUserId"=>"CUSTOMER_UNIQUE_ID", 'amount' => 1000, // Amount in paisa (10 INR) 'redirectUrl'=>"https://webhook.site/redirect-url", 'redirectMode'=>"POST", 'callbackUrl'=>"https://webhook.site/redirect-url", "merchantOrderId"=> "YOUR_ORDER_ID", "mobileNumber"=>"CUSTMER_MOBILE_NUMBER", "message"=>"Order description", "email"=>"CUSTMER_EMAIL_ID", "shortName"=>"CUSTMER_Name", "paymentInstrument"=> array( "type"=> "PAY_PAGE", ) ); |
Step 3: Convert the JSON Payload to Base64 Encoded Payload
2 3 4 5 |
$jsonencode = json_encode($paymentData); $payloadMain = base64_encode($jsonencode); |
Step 4: Calculate X-Verify/Checksum header
2 3 4 5 6 7 8 |
$salt_index = 1; //key index 1 $payload = $payloadMain . "/pg/v1/pay" . $apiKey; $sha256 = hash("sha256", $payload); $final_x_header = $sha256 . '###' . $salt_index; $request = json_encode(array('request'=>$payloadMain)); |
👍
UAT OR SANDBOX API
UAT Host URL: https://api-preprod.phonepe.com/apis/pg-sandbox
API End Point: /pg/v1/payUAT PAY API URL: https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay
Are you want to get implementation help, or modify or extend the functionality of this script?
A Tutorialswebsite Expert can do it for you.
Step 5: Complete PHP code to Integrate PhonePe Payment Gateway
You will get test/sandbox API details from this link: https://developer.phonepe.com/v1/docs/uat-testing
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
<?php // Replace these with your actual PhonePe API credentials $merchantId = 'PGTESTPAYUAT'; // sandbox or test merchantId $apiKey="099eb0cd-02cf-4e2a-8aca-3e6c6aff0399"; // sandbox or test APIKEY $redirectUrl = 'payment-success.php'; // Set transaction details $order_id = uniqid(); $name="Tutorials Website"; $email="info@tutorialswebsite.com"; $mobile=9999999999; $amount = 10; // amount in INR $description = 'Payment for Product/Service'; $paymentData = array( 'merchantId' => $merchantId, 'merchantTransactionId' => "MT7850590068188104", // test transactionID "merchantUserId"=>"MUID123", 'amount' => $amount*100, 'redirectUrl'=>$redirectUrl, 'redirectMode'=>"POST", 'callbackUrl'=>$redirectUrl, "merchantOrderId"=>$order_id, "mobileNumber"=>$mobile, "message"=>$description, "email"=>$email, "shortName"=>$name, "paymentInstrument"=> array( "type"=> "PAY_PAGE", ) ); $jsonencode = json_encode($paymentData); $payloadMain = base64_encode($jsonencode); $salt_index = 1; //key index 1 $payload = $payloadMain . "/pg/v1/pay" . $apiKey; $sha256 = hash("sha256", $payload); $final_x_header = $sha256 . '###' . $salt_index; $request = json_encode(array('request'=>$payloadMain)); $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $request, CURLOPT_HTTPHEADER => [ "Content-Type: application/json", "X-VERIFY: " . $final_x_header, "accept: application/json" ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $res = json_decode($response); if(isset($res->success) && $res->success=='1'){ $paymentCode=$res->code; $paymentMsg=$res->message; $payUrl=$res->data->instrumentResponse->redirectInfo->url; header('Location:'.$payUrl) ; } } ?> |
Remember to replace ‘YOUR_API_KEY’, ‘YOUR_MERCHANT_ID’, ‘YOUR_ORDER_ID’, ‘YOUR_REDIRECT_URL’, and other placeholders with your actual credentials and data.
This is just a basic starting point; you’ll need to handle the response from PhonePe on the callback page payment-success.php and implement the necessary logic to complete the payment flow according to PhonePe’s API documentation.
Output
PhonePe Transaction details After Success Payment on (payment-success.php)
Wrapping Words
These are the simple and easy steps to Integrate PhonePe Payment Gateway in PHP. You can modify and customize code as per your requirements.
Do you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Related Article
Integrate Recurring Stripe Subscription Payment with PHP
Integrate Blockonomics bitcoin payment gateway in PHP
FAQs
Adding a PhonePe gateway to a website isn’t just about using PHP code. The process involves several steps, like getting a merchant account for handling payments, getting essential API credentials, and setting up how the payment gateway works.
You will get test/sandbox API details from this link: https://developer.phonepe.com/v1/docs/uat-testing
Debit Card
“card_number”: “4622943126146407”,
“card_type”: “DEBIT_CARD”,
“card_issuer”: “VISA”,
“expiry_month”: 12,
“expiry_year”: 2027,
“cvv”: “936”
Credit Card
“card_number”: “4208585190116667”,
“card_type”: “CREDIT_CARD”,
“card_issuer”: “VISA”,
“expiry_month”: 06,
“expiry_year”: 2027,
“cvv”: “508”
Yes, You can integrate PhonePe Payment Gateway API In PHP. Please follow this link.
Register your Business with PhonePe Gateway Solution to send money or make payments & get access to over 47+ Crore users. Click Here
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co